package com.ruoyi.framework.websocket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.annotation.Resource;

/**
 * websocket 配置
 * 
 * @author ruoyi
 */
@Configuration
public class WebSocketConfig implements WebSocketConfigurer
{
    @Resource
    private WebSocketUsers webSocketServer;
    @Resource
    private PortalHandshakeInterceptor myHandshakeInterceptor;
    @Bean
    public ServerEndpointExporter serverEndpointExporter()
    {
        return new ServerEndpointExporter();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 配置 WebSocket 处理器和拦截器
        // TODO: 配置 WebSocket 处理器和拦截器 前端需要请求头，这里还不知道怎么处理
        registry.addHandler(new MyWebSocketHandler(), "/websocket/message")
                .addInterceptors(new PortalHandshakeInterceptor()) // 添加握手拦截器
                .setAllowedOrigins("*"); // 允许所有来源的连接
    }
}
